//
//  NetworkTools.swift
//  Sina
//
//  Created by 王杰 on 15/11/16.
//  Copyright © 2015年 王杰. All rights reserved.
//

import UIKit
import AFNetworking
private let  weiboDomain = "com.baidu.data.error"
enum HTTPMethod:String {

    case POST = "POST"
    case GET = "GET"
}
class NetworkTools: AFHTTPSessionManager {

    static let sharedTools:NetworkTools = {
    
        let urlString = "https://api.weibo.com/"
        let url = NSURL(string: urlString)
        let tools = NetworkTools(baseURL: url)
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
    return tools
        
    }()
    
    func requestJsonDict(method:HTTPMethod,urlString:String,parmaters:[String:AnyObject]?,finished:(result:[String:AnyObject]?,error:NSError?) ->()){
   
        if method == HTTPMethod.GET {
        
            GET(urlString, parameters: parmaters, success: { (_, result) -> Void in
                
                if let dict = result as? [String:AnyObject] {

                    finished(result: dict, error: nil)
                    return
                }
                
                //如果数据格式不匹配 返回自定义errorcode值
                
                let dataEarror = NSError(domain: weiboDomain, code: -10000, userInfo: [NSLocalizedDescriptionKey:"数据有误"])
                finished(result: nil, error: dataEarror)
                }){ (_, error) -> Void in
                    //执行错误的回调
                    finished(result: nil, error: error)
                    print(error)
            }

        } else {
            POST(urlString, parameters: parmaters, success: { (_, result) -> Void in
                //校验数据是否是字典
                if let dict = result as? [String : AnyObject] {
                    //执行成功的回调
                    finished(result: dict, error: nil)
                    return
                }

                let dataError = NSError(domain: weiboDomain, code: -10000, userInfo: [NSLocalizedDescriptionKey : "数据错误"])
                finished(result: nil, error: dataError)
                }) { (_, error) -> Void in
                    //执行错误的回调
                    finished(result: nil, error: error)
                    print(error)
            }
        }
    
    }
    //上传单张图片的请求
    func uploadImage(urlString: String,parmaters: [String : AnyObject]?,imageData: NSData,finished: (result: [String : AnyObject]?, error: NSError?) -> ()) {

        POST(urlString, parameters: parmaters, constructingBodyWithBlock: { (multipartFormData) -> Void in
            //4.上传文件的格式  image/jpeg
            multipartFormData.appendPartWithFileData(imageData, name: "pic", fileName: "OMG", mimeType: "image/jpeg")
            
            }, success: { (_, result) -> Void in
                if let dict = result as? [String: AnyObject] {
                
                finished(result: dict, error: nil)
                    
                }
            }) { (_, error) -> Void in
                
                finished(result: nil, error: error)
                
                print(error)
        }
    }
}

















